/**
 * 文件名：productServiceImpl.java
 * 
 * 版本信息：
 * 日期：2013-12-5
 * Copyright 足下 Corporation 2013
 * 版权所有
 * 
 */
package com.hugedata.cdn.outerfacade.serviceadapter;

import java.util.List;
import java.util.ResourceBundle;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.req.NodeOnConditionReq;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.req.NodeStatusReq;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.req.TaskOnConditionReq;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.request.DelTaskRequest;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.request.ModTaskStatusRequest;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.resp.IDCChildNode;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.resp.IDCChildTask;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.resp.LmnChildNode;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.resp.LmnChildTask;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.resp.MnOnlineChildStatus;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.resp.NodeOnConditionChildResponse;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.resp.NodeStatusChildResponse;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.resp.TaskChildResponse;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.resp.TaskOnConditionChildResponse;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.response.DelTaskResponse;
import com.hugedata.cdn.service.task.impl.TaskPushService;
import com.hugedata.cdnserver.util.AdvanceCollectionUtils;
import com.hugedata.imsp.jabberc.IMSPClient;
import com.hugedata.imsp.jabberc.service.node.IDCNode;
import com.hugedata.imsp.jabberc.service.node.LmnNode;
import com.hugedata.imsp.jabberc.service.node.MnOnlineStatus;
import com.hugedata.imsp.jabberc.service.node.NodeOnConditionRequest;
import com.hugedata.imsp.jabberc.service.node.NodeOnConditionResponse;
import com.hugedata.imsp.jabberc.service.node.NodeStatusRequest;
import com.hugedata.imsp.jabberc.service.node.NodeStatusResponse;
import com.hugedata.imsp.jabberc.service.task.IDCTask;
import com.hugedata.imsp.jabberc.service.task.LmnTask;
import com.hugedata.imsp.jabberc.service.task.TaskOnConditionResponse;

/**
 * 
 * 项目名称：cdn 类名称：productServiceImpl 类描述： 创建人：zhaoaihua 创建时间：2013-12-5 上午10:22:02
 * 修改备注：
 * 
 * @version
 * 
 */
public class productServiceImpl implements productService
{
    private Logger logger = Logger.getLogger(productServiceImpl.class);

    private TaskPushService taskPushService;

    public productServiceImpl(TaskPushService taskPushService)
    {
        this.taskPushService = taskPushService;
    }

    // ----------------------ad by wxm---------------------------start-------
    // 产品查询节点离在线记录
    public NodeStatusChildResponse getNodeStatus(NodeStatusReq req)
    {
        NodeStatusChildResponse resp = new NodeStatusChildResponse();
        NodeStatusRequest request = new NodeStatusRequest();
        try
        {
            BeanUtils.copyProperties(request, req);
            NodeStatusResponse response = IMSPClient.getInstance()
                    .getNodeStatus(request);
            BeanUtils.copyProperties(resp, response);

            List<MnOnlineStatus> mnOnlineStatusList = response
                    .getMnOnlineStatusList();
            int mnLen = AdvanceCollectionUtils.getLength(mnOnlineStatusList);
//            int mnLen = CollectionUtils.size(mnOnlineStatusList);
            MnOnlineChildStatus[] mnOnlineStatusArr = new MnOnlineChildStatus[mnLen];
            for (int i = 0; i < mnLen; i++)
            {
                MnOnlineStatus tempStatus = mnOnlineStatusList.get(i);
                MnOnlineChildStatus tempChildStatus = new MnOnlineChildStatus();
                BeanUtils.copyProperties(tempChildStatus, tempStatus);

                List<List<String>> statusDataList = tempStatus
                        .getMnStatusData();
                int tempLen = AdvanceCollectionUtils.getLength(statusDataList);
                String[] mnStatusDataArr = new String[tempLen];
                for (int j = 0; j < tempLen; j++)
                {
                    List<String> tempList = statusDataList.get(j);
                    String tempMnStatusData = StringUtils.join(tempList, ",");
                    mnStatusDataArr[j] = tempMnStatusData;
                }
                tempChildStatus.setMnStatusDataArr(mnStatusDataArr);
                mnOnlineStatusArr[i] = tempChildStatus;
            }
            resp.setMnOnlineStatusArr(mnOnlineStatusArr);
        }
        catch (Exception e)
        {
            logger.error("", e);
            throw new RuntimeException(e);
        }
        return resp;
    }

    // 产品按工具区域运营商查询节点数
    public NodeOnConditionChildResponse getNodesByToolAndISP(
            NodeOnConditionReq req)
    {
        NodeOnConditionChildResponse resp = new NodeOnConditionChildResponse();
        try
        {
            NodeOnConditionRequest request = new NodeOnConditionRequest();
            BeanUtils.copyProperties(request, req);
            NodeOnConditionResponse response = IMSPClient.getInstance()
                    .getNodesByToolAndISP(request);

            List<IDCNode> idcList = response.getIdcList();
            if (CollectionUtils.isNotEmpty(idcList))
            {
                int idcLen = CollectionUtils.size(idcList);
                IDCChildNode[] idcArr = new IDCChildNode[idcLen];
                for (int i = 0; i < idcLen; i++)
                {
                    IDCChildNode tempNode = new IDCChildNode();
                    BeanUtils.copyProperties(tempNode, idcList.get(i));
                    idcArr[i] = tempNode;
                }
                resp.setIdcArr(idcArr);
            }

            List<LmnNode> lmnList = response.getLmnList();
            if (CollectionUtils.isNotEmpty(lmnList))
            {
                int lmnLen = CollectionUtils.size(lmnList);
                LmnChildNode[] lmnArr = new LmnChildNode[lmnLen];
                for (int i = 0; i < lmnLen; i++)
                {
                    LmnChildNode tempNode = new LmnChildNode();
                    BeanUtils.copyProperties(tempNode, lmnList.get(i));
                    lmnArr[i] = tempNode;
                }
                resp.setLmnArr(lmnArr);
            }

            BeanUtils.copyProperties(resp, response);
        }
        catch (Exception e)
        {
            logger.error("", e);
            throw new RuntimeException(e);
        }
        return resp;
    }

    // 产品按区域运营商创建任务
    public TaskOnConditionChildResponse createTaskByArea(TaskOnConditionReq req)
    {
        TaskOnConditionChildResponse resp = new TaskOnConditionChildResponse();
        Gson gson = new Gson();
        try
        {
            TaskOnConditionResponse response = this.taskPushService
                    .createTaskByArea(req);
            BeanUtils.copyProperties(resp, response);

            List<IDCTask> idcTasks = response.getIdcTasks();
            if (CollectionUtils.isNotEmpty(idcTasks))
            {
                int idcLen = CollectionUtils.size(idcTasks);
                IDCChildTask[] idcTaskArr = new IDCChildTask[idcLen];
                for (int i = 0; i < idcLen; i++)
                {
                    IDCChildTask tempTask = new IDCChildTask();
                    IDCTask oldTempTask = idcTasks.get(i);
                    String taskInfoJsonStr = gson.toJson(oldTempTask
                            .getTaskList());
                    tempTask.setTaskInfoJsonStr(taskInfoJsonStr);
                    BeanUtils.copyProperties(tempTask, oldTempTask);
                    idcTaskArr[i] = tempTask;
                }
                resp.setIdcTaskArr(idcTaskArr);
            }

            List<LmnTask> lmnTasks = response.getLmnTasks();
            if (CollectionUtils.isNotEmpty(lmnTasks))
            {
                int lmnLen = CollectionUtils.size(lmnTasks);
                LmnChildTask[] lmnTaskArr = new LmnChildTask[lmnLen];
                for (int i = 0; i < lmnLen; i++)
                {
                    LmnChildTask tempTask = new LmnChildTask();
                    LmnTask oldTempTask = lmnTasks.get(i);
                    String taskInfoJsonStr = gson.toJson(oldTempTask
                            .getTaskList());
                    tempTask.setTaskInfoJsonStr(taskInfoJsonStr);
                    BeanUtils.copyProperties(tempTask, oldTempTask);
                    lmnTaskArr[i] = tempTask;
                }
                resp.setLmnTaskArr(lmnTaskArr);
            }
        }
        catch (Exception e)
        {
            logger.error("", e);
            throw new RuntimeException(e);
        }
        return resp;
    }

    // 产品删除任务请求
    public TaskChildResponse deleteTaskByArea(DelTaskRequest req)
    {
        TaskChildResponse resp = new TaskChildResponse();
        DelTaskResponse response = new DelTaskResponse();
        try
        {
            logger.info("begin deleteTaskByArea-------------------taskId="
                    + req.getTaskId());
            taskPushService.delTask(req, response);
            // TaskRequest taskRequest = new TaskRequest();
            // taskRequest.setTaskId(req.getTaskId());
            resp.setResultcode(0);
            // TaskResponse taskResponse =
            // IMSPClient.getInstance().deleteTaskByArea(taskRequest);
            // BeanUtils.copyProperties(resp, taskResponse);
        }
        catch (Exception e)
        {
            logger.error("deleteTaskByArea failed", e);
            throw new RuntimeException(e);
        }
        return resp;
    }

    // 修改任务
    public TaskOnConditionChildResponse modTask(TaskOnConditionReq request)
    {
        TaskOnConditionChildResponse resp = new TaskOnConditionChildResponse();
        try
        {
            TaskOnConditionResponse response = this.taskPushService
                    .modTask(request);
            BeanUtils.copyProperties(resp, response);
        }
        catch (Exception e)
        {
            logger.error("", e);
            throw new RuntimeException(e);
        }
        return resp;
    }

    // 暂停任务
    public TaskChildResponse pauseTask(DelTaskRequest req)
    {
        ModTaskStatusRequest request = new ModTaskStatusRequest();
        request.setTaskId(req.getTaskId());
        try
        {

            TaskChildResponse rep = this.taskPushService.pauseTask(request);

            int resultCode = rep.getResultcode();
            if (0 == resultCode)
            {
                logger.info("Pause Task SUCCESS taskId: " + req.getTaskId());
            }
            else
            {
                logger.info("Pause Task SUCCESS FAILURE: " + req.getTaskId());
            }
            return rep;

        }
        catch (Exception e)
        {
            logger.error("", e);
            throw new RuntimeException(e);
        }
    }

    // 启动任务
    public TaskOnConditionChildResponse resumeTask(DelTaskRequest request)
    {
        TaskOnConditionChildResponse result = new TaskOnConditionChildResponse();
        ModTaskStatusRequest req = new ModTaskStatusRequest();
        req.setTaskId(request.getTaskId());

        try
        {
            TaskOnConditionResponse resp = this.taskPushService.resumeTask(req);
            int resultCode = resp.getResultcode();
            if (0 == resultCode)
            {
                logger.info("Start Task SUCCESS taskId: " + request.getTaskId());
            }
            else
            {
                logger.info("Start Task FAILURE taskId: " + request.getTaskId());
            }

            BeanUtils.copyProperties(result, resp);
            return result;
        }
        catch (Exception e)
        {
            logger.error("", e);
            throw new RuntimeException(e);
        }
    }

    // ----------------------ad by wxm---------------------------start-------

    public String test123(String str)
    {
        return str + "123";
    }
}
